'use strict';
exports = module.exports = function ($scope, $rootScope, $timeout, $state, $uibModal, yxtable14, $compile, $alert, throttleSubmit) {
    ///////////////////////////////
    $scope.fromObj = {
        level: [
            {title: "收入分配"},
            {title: "二业务"}
        ],
        tab: [
            {title:"浏览器", link:"#/income/busi_brower", me:true},
            {title:"下载器", link:"#/income/busi_down"}
        ],
        searchBtn: [ // 搜索附加按钮
            {title:"同步数据", name:"synchronization", func:"确认"},
            {title:"导出", name:"export", func:"下载"},
            {title:"批量处理单价", name:"batchEditPrice", func:"弹框"},
            {title:"批量处理数量", name:"batchEditNumber", func:"弹框"},
            {title:"手动校准", name:"correct", func:"确认"},
            {title:"开启自动校准", name:"autoCorrect", func:"确认"}
        ],
        opBtn: [ // 操作
            {title:"调整", name:"adjustment", func:"弹框"},
            {title:"编辑", name:"eidt", func:"跳转"}
        ],
        dropApi: [
            { title:"用户", api:"Customer", url:"api/customer/getlist" },
            { title:"业务", api:"BusiType", url:"api/busi/typelist" },
            { title:"归档状态", api:"ArchiveStatus", staticData:`{"0":"未归档","1":"已归档"}` },
            { title:"收入状态", api:"IncomeStatus", staticData:`{"0":"预估","1":"暂定","2":"封账"}` },
            { title:"拆分状态", api:"SplitStatus", staticData:`{'-1':"失败","1":"成功","0":"初始状态"}` }
        ],
        search: [
            {title:"begin|end",name:"时间段",type:"日期范围",api:""},
            {title:"keyWord",name:"关键字",type:"文本框",api:""},
            {title:"username",name:"用户",type:"下拉框",dropSearch:true,api:"Customer"},
            {title:"bizId",name:"分类",type:"下拉框",dropSearch:true,api:"BusiType"},
            {title:"archive",name:"归档状态",type:"下拉框",api:"ArchiveStatus"},
            {title:"incomeStatus",name:"收入状态",type:"下拉框",api:"IncomeStatus"},
            {title:"splitState",name:"拆分状态",type:"下拉框",api:"SplitStatus"}
        ],
        tableColume: [
            {title:"date",name:"产出日期",type:"文本",mock:"日期"},
            {title:"archiveDate",name:"归档日期",type:"文本",mock:"日期"},
            {title:"userName",name:"用户名",type:"文本",mock:"名字"},
            {title:"bizName",name:"业务名称",type:"文本",mock:"词语"},
            {title:"adId",name:"推广id",type:"文本",mock:"id"},
            {title:"url",name:"链接",type:"文本",mock:"网址"},
            {title:"uvCount",name:"UV数",type:"文本",mock:"数字",sum:true},
            {title:"uvAdjustCount",name:"调整后的UV数",type:"文本",mock:"数字",sum:true},
            {title:"price",name:"收入单价",type:"文本",mock:"金钱"},
            {title:"amount",name:"收入金额",type:"文本",mock:"金钱",sum:true},
            {title:"xxx",name:"收入总金额",type:"文本",mock:"金钱"},
            {title:"incomeType",name:"收入状态",type:"选择",option:`{"0":"预估","1":"暂定","2":"封账"}`,mock:"枚举3"},
            {title:"archiveState",name:"归档状态",type:"选择",option:`{"0":"未归档","1":"已归档"}`,mock:"枚举2"},
            {title:"splitState",name:"拆分状态",type:"选择",option:`{'-1':"失败","1":"成功","0":"初始状态"}`,mock:"枚举3"}
        ],
        listApi: {
            "url":"api/Allot/IncomeB2TBList",
            "api":"IncomeB2TB"
        }
    };
    $scope.addItem = function(objName) {
        $scope.fromObj[objName].push({})
    };
    $scope.removeItem = function(objName, index) {
        $scope.fromObj[objName].splice(index, 1)
    };
    ///////////////////////////////
    var templateA = "";
    var template$API = "";
    var templateJ = "";
    var templateH = "";
    var $API = {};
    $scope.produceCode = function() {
        var json = $scope.fromObj;
        (function() {
            templateA += `
                                                                                                    $API.List = {};
                                                                                                    $API.Drop = {};
                                                                                                    var $ajax = {};
                                                                                                    $ajax.sent = function(param) {
                                                                                                        return new Promise((resolve, reject) => {
                                                                                                            if(param.url == '${ json.listApi.url }') {
                                                                                                                var mock = {
                                                                                                                    "code": 200,
                                                                                                                    "msg":"",
                                                                                                                    "data": {
                                                                                                                        "list": [],
                                                                                                                        "rowcount": 0,
                                                                                                                        "sumDate": {}
                                                                                                                    }
                                                                                                                };
                                                                                                                var Random = Mock.Random;
                                                                                                                var rowcount = Random.integer(1, 100);
                                                                                                                var pagecount = ( rowcount > 10 ) ? 10 : rowcount;
                                                                                                                for(var i = 0, l = pagecount; i < l; i++) {
                                                                                                                    mock.data.list.push({`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var mock = one.mock.toLowerCase();
                if (mock.indexOf('日期') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.date('yyyy-MM-dd'),`
                } else if (mock.indexOf('名字') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.cname(),`
                } else if (mock.indexOf('词语') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.cparagraph(1),`
                } else if (mock.indexOf('id') > -1 || mock.indexOf('数字') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.natural(1, 10000),`
                } else if (mock.indexOf('网址') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.url(),`
                } else if (mock.indexOf('枚举2') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.integer(0, 1),`
                } else if (mock.indexOf('枚举3') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.integer(0, 2),`
                } else if (mock.indexOf('枚举4') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.integer(0, 3),`
                } else if (mock.indexOf('ip') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.ip(),`
                } else if (mock.indexOf('金钱') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.float(1, 10000, 2, 2),`
                } else if (mock.indexOf('邮箱') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.float(1, 10000, 2, 2),`
                } else if (mock.indexOf('地址') > -1) {
                    templateA += `
                                                                                                                        "${ title }": Random.email(),`
                } else { // 其他
                    templateA += `
                                                                                                                        "${ title }": Random.string('lower', 3, 10),`
                }
            }
            templateA = templateA.replace(/,$/, '');
            templateA += `
                                                                                                                    })
                                                                                                                }
                                                                                                                mock.data.rowcount = rowcount;
                                                                                                                (function() {
                                                                                                                    mock.data.sumDate = {`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                if(!one.sum) {continue;}
                json.tableSum = true;
                var title = one.title;
                var mock = one.mock.toLowerCase();
                if (mock.indexOf('日期') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.date('yyyy-MM-dd'),`
                } else if (mock.indexOf('名字') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.cname(),`
                } else if (mock.indexOf('词语') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.cparagraph(1),`
                } else if (mock.indexOf('id') > -1 || mock.indexOf('数字') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.natural(1, 10000),`
                } else if (mock.indexOf('网址') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.url(),`
                } else if (mock.indexOf('枚举2') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.integer(0, 1),`
                } else if (mock.indexOf('枚举3') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.integer(0, 2),`
                } else if (mock.indexOf('枚举4') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.integer(0, 3),`
                } else if (mock.indexOf('ip') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.ip(),`
                } else if (mock.indexOf('金钱') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.float(1, 10000, 2, 2),`
                } else if (mock.indexOf('邮箱') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.float(1, 10000, 2, 2),`
                } else if (mock.indexOf('地址') > -1) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.email(),`
                } else { // 其他
                    templateA += `
                                                                                                                        "${ title }Sum": Random.string('lower', 3, 10),`
                }
            }
            templateA = templateA.replace(/,$/, '');
            templateA += `
                                                                                                                    }
                                                                                                                })()
                                                                                                                resolve(mock);
                                                                                                                return
                                                                                                            }`
            for(var i = 0, l = json.dropApi.length; i < l; i++) {
                var one = json.dropApi[i];
                var url = one.url;
                if(url) {
                    templateA += `
                                                                                                            if(param.url == '${ url }') {
                                                                                                                var mock = {
                                                                                                                    "code": 200,
                                                                                                                    "msg":"",
                                                                                                                    "data": {
                                                                                                                        "list": [
                                                                                                                            {"id": 11, "name": "假数据1"},
                                                                                                                            {"id": 21, "name": "假数据2"},
                                                                                                                            {"id": 31, "name": "假数据3"},
                                                                                                                            {"id": 41, "name": "假数据4"},
                                                                                                                            {"id": 51, "name": "假数据5"},
                                                                                                                            {"id": 61, "name": "假数据6"}
                                                                                                                        ]
                                                                                                                    }
                                                                                                                };
                                                                                                                resolve(mock);
                                                                                                                return;
                                                                                                            }`
                }
            }
            templateA += `
                                                                                                        });
                                                                                                    };
                                                                                                    $API.List.${ json.listApi.api } = function(params) {
                                                                                                        return new Promise(function(resolve, reject) {
                                                                                                            \/\/ 处理默认值 同时进行弥补处理`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                if(title.indexOf("|")>-1) {
                    var ia = title.split("|");
                    for(var ii = 0, li = ia.length; ii < li; ii++) {
                        templateA += `
                                                                                                            params.${ ia[ii] } = params.${ ia[ii] };`
                    }
                } else {
                    templateA += `
                                                                                                            params.${ title } = params.${ title };`
                }
            }
            templateA += `
                                                                                                            $ajax.sent({
                                                                                                                method:'GET',
                                                                                                                url:'${ json.listApi.url }',
                                                                                                                params:params
                                                                                                            }).then(function(res) {
                                                                                                                resolve(res.data)
                                                                                                            });
                                                                                                        });
                                                                                                    }`
            for(var i = 0, l = json.dropApi.length; i < l; i++) {
                var one = json.dropApi[i];
                var title = one.title;
                var api = one.api;
                var url = one.url;
                var staticData = '';
                try{
                    staticData = eval('('+ one.staticData +')');
                }catch(e) {
                    alert('error1036');
                    console.error("json格式有问题", one.staticData, e);
                }
                templateA += `
                                                                                                    $API.Drop.${ api } = function(opt, params) {
                                                                                                        return new Promise(function(resolve, reject) {
                                                                                                            var nosearch = opt.indexOf('nosearch') > -1`
                if(url) {
                    templateA += `
                                                                                                            var noall = opt.indexOf('noall') > -1
                                                                                                            var arr = noall ? [{id: 0, name:"全部${ title }"}] :[];
                                                                                                            $ajax.sent({
                                                                                                                method: 'GET',
                                                                                                                url: '${ url }',
                                                                                                                params: {
                                                                                                                    // 传参
                                                                                                                }
                                                                                                            }).then(function(res) {
                                                                                                                resolve(select2Config( arr.concat(res.data||[]), !nosearch ))
                                                                                                            });`
                } else {
                    templateA += `
                                                                                                            var noall = opt.indexOf('noall') > -1
                                                                                                            var arr = noall ? [{id: 0, name:"全部${ title }"}] :[];
                                                                                                            var arr2 = [`
                    for(var ll in staticData) {
                        templateA += `
                                                                                                                {"id": ${ ll },"name":"${ staticData[ll] }" },`
                    }
                    templateA = templateA.replace(/,$/, '');
                    templateA += `
                                                                                                            ];
                                                                                                            resolve(select2Config( arr.concat(arr2), 0 ))`
                }
                templateA += `
                                                                                                        });
                                                                                                    };`
            }
            eval(templateA);
        })();
        (function() {
            templateJ += `
                                                                                                    // 搜索表单项
                                                                                                    $scope.obj = {`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                var name = one.name;
                templateJ += `
                                                                                                        \/\/ 下拉框搜索项: ${ name }`
                if(title.indexOf("|")>-1) {
                    var ia = title.split("|");
                    for(var ii = 0, li = ia.length; ii < li; ii++) {
                        templateJ += `
                                                                                                        ${ ia[ii] }: '',`
                    }
                } else {
                    templateJ += `
                                                                                                        ${ title }: '',`
                }
            }
            templateJ = templateJ.replace(/,$/, '');
            templateJ += `
                                                                                                    }
                                                                                                    \/\/ 初始化数据源
                                                                                                    $scope.dataSet = {`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var api = one.api;
                if( !api ) {continue;}
                var name = one.name;
                templateJ += `
                                                                                                        ${ api }: select2Config( [] ), \/\/ 数据源: ${ name }列表`
            }
            templateJ = templateJ.replace(/,$/, '');
            templateJ += `
                                                                                                    }`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var type = one.type;
                var isSelect = type.indexOf('下拉框') > -1
                if( !isSelect ) {continue;}
                var name = one.name;
                var api = one.api;
                if(!api) {
                    alert('error1033');
                    console.error("未输入api in " + property);
                    continue;
                }
                templateJ += `
                                                                                                    \/\/ 读取${ name }列表
                                                                                                    $API.Drop.${ api }('all.search')
                                                                                                    .then(function(data) { $timeout(function() {
                                                                                                            $scope.dataSet.${ api } = data
                                                                                                        }, 0) }
                                                                                                    );`
            }
            templateJ += `
                                                                                                    \/\/ 表格数据
                                                                                                    $scope.page = 1; // 分页
                                                                                                    $scope.pagesize = 30;//每页显示条数，默认30条;
                                                                                                    $scope.yxt14loading = false; // 加载中
                                                                                                    $scope.yxt14totalrow = 9999; // 总条数
                                                                                                    $scope.yxt14datalist = [];
                                                                                                    $scope.yxt14datasum = {};
                                                                                                    \/\/ 读接口
                                                                                                    $scope.showList = function() {
                                                                                                        $scope.yxt14loading = true;
                                                                                                        $API.List.${ json.listApi.api }($scope.params).then(function(data) {
                                                                                                            $timeout(function() {
                                                                                                                $scope.yxt14datalist = data.list;
                                                                                                                $scope.yxt14totalrow = data.rowcount;
                                                                                                                $scope.yxt14datasum = data.sumDate;
                                                                                                                $scope.yxt14loading = false;
                                                                                                            }, 0)
                                                                                                        });
                                                                                                    }
                                                                                                    // 初始化
                                                                                                    yxtable14.init($scope);`
            for(var i = 0, l = json.opBtn.length; i < l; i++) {
                var one = json.opBtn[i];
                var title = one.title;
                var name = one.name;
                var func = one.func;
                templateJ += `
                                                                                                    $scope.${ name }Item = function(item) { \/\/ ${ title }
                                                                                                        \/\/ todo
                                                                                                        alert("这里应该是 ${ func } 处理对象");
                                                                                                    }`
            }
            for(var i = 0, l = json.searchBtn.length; i < l; i++) {
                var one = json.searchBtn[i];
                var title = one.title;
                var name = one.name;
                var func = one.func;
                templateJ += `
                                                                                                    $scope.${ name }Item = function() { \/\/ ${ title }
                                                                                                        \/\/ TODO :: 这里应该是 ${ func }操作`
                if( func == '确认' ) {
                    templateJ += `
                                                                                                        $alert.confirm("是否进行${ title }", function(closeFn) {
                                                                                                            console.log("开始执行${ title }");
                                                                                                            closeFn(); \/\/ 关闭窗口
                                                                                                        })`
                } else if( func == '下载' ) {
                    templateJ += `
                                                                                                        var url = "http://www.baidu.com?t=" + Math.ceil(Math.random()*1e4);`
                    for(var ib = 0, lb = json.search.length; ib < lb; ib++) {
                        var one = json.search[ib];
                        var title = one.title;
                        var name = one.name;
                        if(title.indexOf("|")>-1) {
                            var ia = title.split("|");
                            for(var ii = 0, li = ia.length; ii < li; ii++) {
                                templateJ += `
                                                                                                        url += "&${ ia[ii] }=" + $scope.obj.${ ia[ii] }; \/\/ ${ name }`
                            }
                        } else {
                            templateJ += `
                                                                                                        url += "&${ title }=" + $scope.obj.${ title }; \/\/ ${ name }`
                        }
                    }
                    templateJ += `
                                                                                                        window.location.href = url`
                } else if( func == '弹框' ) {
                    templateJ += `
                                                                                                        $uibModal.open({
                                                                                                            templateUrl : '${ name }Item.html',
                                                                                                            controller : function($scope, $uibModalInstance, $timeout, throttleSubmit){
                                                                                                                $scope.frm = {
                                                                                                                    a1 : '',
                                                                                                                    a2 : '',
                                                                                                                    a3 : '',
                                                                                                                    a4 : ''
                                                                                                                };
                                                                                                                $scope.frm2 = {
                                                                                                                    nameSource : select2Config([])
                                                                                                                };
                                                                                                                $scope.submitFun = throttleSubmit(function(){
                                                                                                                    $scope.From.submitted = true;
                                                                                                                    if( $scope.From.$invalid ) {
                                                                                                                        $alert.error('请检查提交的信息')
                                                                                                                        return false;
                                                                                                                    }
                                                                                                                    return param
                                                                                                                },function(enable, param){
                                                                                                                    console.log('提交代码', param)
                                                                                                                    $timeout(function(){
                                                                                                                        $uibModalInstance.dismiss();
                                                                                                                        enable()
                                                                                                                    },3e3)
                                                                                                                })
                                                                                                                $scope.Cancel = function() {
                                                                                                                    $uibModalInstance.dismiss();
                                                                                                                };
                                                                                                            },
                                                                                                            windowClass: 'modalWidth700',
                                                                                                            scope:$scope.$new()
                                                                                                        });`
                }
                templateJ += `
                                                                                                    }`
            }
            eval(templateJ);
        })();
        (function() {
            templateH += `
                                                                                                    <div class="yxt14breadcrumb">
                                                                                                        <a href="javascript:;">当前位置 : </a>`
            for(var i = 0, l = json.level.length; i < l; i++) {
                var title = json.level[i].title
                templateH += `
                                                                                                        <a href="javascript:;"> ${ title }</a>`
            }
            var title_last = json.level[ json.level.length -1 ].title
            templateH += `
                                                                                                    </div>
                                                                                                    <div class="container-fluid">
                                                                                                        <div class="yxt14main panel">
                                                                                                            <div class="yxt14title"><i></i>&nbsp;&nbsp;${ title_last }</div>
                                                                                                            <div class="yxt14nav">`
            for(var i = 0, l = json.tab.length; i < l; i++) {
                var one = json.tab[i];
                var title = one.title;
                var link = one.link;
                var me = one.me?'yxt14active':'';
                templateH += `
                                                                                                                <a class="yxt14navson ${ me }" ng-click="goto('${ link }')">${ title }</a>`
            }
            templateH += `
                                                                                                            </div>
                                                                                                            <form class="form-inline">`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                var type = one.type;
                var name = one.name;
                var isDaterange = type.indexOf('日期范围') > -1;
                var isInput = type.indexOf('文本框') > -1;
                var isSelect = type.indexOf('下拉框') > -1;
                templateH += `
                                                                                                                <div class="form-group">`
                if (isSelect) {
                    var api = one.api;
                    if (!api) {
                        alert('error1036');
                        console.error("未输入api in " + property);
                    }
                    templateH += `
                                                                                                                    <input select2 config="dataSet.${ api }" ng-model="obj.${ title }"
                                                                                                                        select2-model='dataSet.${ title }' class="form-control" placeholder="全部${ name }"
                                                                                                                    />`
                } else if (isInput) {
                    templateH += `
                                                                                                                    <input class="form-control" type="text" name="${ title }"
                                                                                                                        ng-model="obj.${ title }" placeholder="请输入${ name }"
                                                                                                                    />`
                } else if (isDaterange) {
                    var iarr = title.split('|');
                    templateH += `
                                                                                                                    <le-date style="width:200px;" bdate="obj.${ iarr[0] }" edate="obj.${ iarr[1] }"
                                                                                                                    ></le-date>`
                }
                templateH += `
                                                                                                                </div>`
            }
            templateH += `
                                                                                                                <div class="form-group">
                                                                                                                    <button ng-click="search()" class="btn yxt14btn-red">搜索</button>
                                                                                                                </div>`
            for(var i = 0, l = json.searchBtn.length; i < l; i++) {
                var one = json.searchBtn[i];
                var title = one.title;
                var name = one.name;
                templateH += `
                                                                                                                <div class="form-group pull-right">
                                                                                                                    <button class="btn yxt14btn-blue" ng-click="${ name }Item()">
                                                                                                                        <i class="glyphicon glyphicon-plus"></i>&nbsp;${ title }
                                                                                                                    </button>
                                                                                                                </div>`
            }
            templateH += `
                                                                                                            <div style="clear:both;"></div>
                                                                                                            </form>
                                                                                                            <div class="table-responsive main-table-striped">
                                                                                                                <table class="table">
                                                                                                                <thead>
                                                                                                                    <tr>`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var name = one.name;
                templateH += `
                                                                                                                        <th class="yxt14left" title="${ name }" style="width:100px;">${ name }</th>`
            }
            if (json.opBtn.length) {
                templateH += `
                                                                                                                        <th class="yxt14center" title="操作" style="width:100px;">操作</th>`
            }
            templateH += `
                                                                                                                    </tr>
                                                                                                                </thead>
                                                                                                                <tbody ng-show="!yxt14datalist.length && !yxt14loading">
                                                                                                                    <tr>
                                                                                                                        <td colspan="99"><center>无数据</center></td>
                                                                                                                    </tr>
                                                                                                                </tbody>
                                                                                                                <tbody ng-repeat="row in yxt14datalist">
                                                                                                                    <tr>`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var type = one.type;
                var switche = type.indexOf('选择') > -1;
                if (switche) {
                    var api = one.option;
                    templateH += `
                                                                                                                        <td class="yxt14center" title="{{ row.${ title } }}">
                                                                                                                            <div class="yxt14ellipsis">
                                                                                                                                {{ ${ api }[row.${ title }]||'-' }}
                                                                                                                            </div>
                                                                                                                        </td>`
                } else { // 其他的都认为是文本
                    templateH += `
                                                                                                                        <td class="yxt14left" title="{{ row.${ title } }}">
                                                                                                                            <div class="yxt14ellipsis">
                                                                                                                                {{ row.${ title } }}
                                                                                                                            </div>
                                                                                                                        </td>`
                }
            }
            if (json.opBtn.length) {
                templateH += `
                                                                                                                        <td class="yxt14center">`
                for(var i = 0, l = json.opBtn.length; i < l; i++) {
                    var one = json.opBtn[i];
                    var name = one.name;
                    var title = one.title;
                    templateH += `
                                                                                                                            <a ng-click="${ name }Item(row)">${ title }</a>`
                }
                templateH += `
                                                                                                                        </td>`
            }
            templateH += `
                                                                                                                    </tr>
                                                                                                                </tbody>`
            if(json.tableSum) {
                templateH += `
                                                                                                                <tbody class="tablefoot">
                                                                                                                    <tr>`
                for(var i = 0, l = json.tableColume.length; i < l; i++) {
                    var one = json.tableColume[i];
                    var title = one.title;
                    if(i==0) {
                        templateH += `
                                                                                                                        <td class="yxt14left"> <b>汇总</b> </td>`
                    } else if ( !one.sum ) {
                        templateH += `
                                                                                                                        <td></td>`
                    } else {
                        templateH += `
                                                                                                                        <td class="yxt14left" title="{{ yxt14datasum.${ title }Sum }}">
                                                                                                                            <b>{{ yxt14datasum.${ title }Sum }}</b>
                                                                                                                        </td>`
                    }
                }
                if (json.opBtn.length) {
                    templateH += `
                                                                                                                        <td></td>`
                }
                templateH += `
                                                                                                                    </tr>
                                                                                                                </tbody>`
            }
            templateH += `
                                                                                                                <tbody ng-show="yxt14loading">
                                                                                                                    <tr class="yxt14loading">
                                                                                                                        <td colspan="99">
                                                                                                                            <center>数据加载中……</center>
                                                                                                                            <div> <div class="sk-spinner sk-spinner-wave"> <div class="sk-rect1"></div> <div class="sk-rect2"></div>
                                                                                                                            <div class="sk-rect3"></div> <div class="sk-rect4"></div> <div class="sk-rect5"></div> </div> </div>
                                                                                                                        </td>
                                                                                                                    </tr>
                                                                                                                </tbody>
                                                                                                                </table>
                                                                                                            </div>
                                                                                                            <div class="main-table-info">
                                                                                                                <div class="main-table-info-msg"> <h6>{{showMessage()}}</h6> </div>
                                                                                                                <div class="pull-right">
                                                                                                                    <uib-pagination total-items="yxt14totalrow" ng-model="page" max-size="3"
                                                                                                                        first-text="第一页" items-per-page="pagesize" ng-change="pageChanged()"
                                                                                                                        previous-text="上一页" next-text="下一页" last-text="最后页"
                                                                                                                        boundary-links="true" boundary-link-numbers="true">
                                                                                                                    </uib-pagination>
                                                                                                                </div>
                                                                                                            </div>
                                                                                                        </div>`
            for(var i = 0, l = json.searchBtn.length; i < l; i++) {
                var one = json.searchBtn[i];
                var name = one.name;
                var func = one.func;
                if( func == '弹框' ) {
                    templateH += `

                                                                                                        <script type="text/ng-template" id="${ name }Item.html" >
                                                                                                            <form class="yxf14" name="From">
                                                                                                                <b class="yxf14-title">重新整理的表单</b>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">输入框</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <input type="text" placeholder="请输入标题" style="width:200px;"
                                                                                                                            ng-model="frm.a1" name="a1" maxlength="21"
                                                                                                                            required ng-minlength="4" ng-maxlength="20">
                                                                                                                        <b ng-show="( From.a1.$dirty || From.submitted ) && From.a1.$invalid"> {{From.a1.$error.required?"必填项":"请输入4-20字符"}} </b>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">下拉框</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <input type="text" select2 style="width:200px;" name="a2"
                                                                                                                            ng-model="frm.a2" config="obj2.nameSource" select2-model="frm2.a2" required
                                                                                                                        />
                                                                                                                        <b ng-show="( From.a2.$dirty || From.submitted ) && From.a2.$invalid"> 必填项 </b>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">领取方式</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a3" convert-to-number value="0"> <i></i>线下核销</label>
                                                                                                                        <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a3" convert-to-number value="1"> <i></i>邮寄</label>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">简介</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <textarea placeholder="请输入简介" style="width:400px;"
                                                                                                                            ng-model="frm.a4" name="a4" rows="3" maxlength="64" required>
                                                                                                                        </textarea>
                                                                                                                        <i ng-hide="( From.a4.$dirty || From.submitted ) && From.a4.$invalid">{{ frm.a4.length || 0 }} / 64</i>
                                                                                                                        <b ng-show="( From.a4.$dirty || From.submitted ) && From.a4.$invalid"> 必填项</b>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <!-- 提交 -->
                                                                                                                <div class="yxf14-box">
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <button type="button" class="btn btn-danger" ng-click="submitFun($event)">提交表单</button>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                            </form>
                                                                                                        </script>`
                }
            }
            templateH += `
                                                                                                    </div>`
            var dom = $compile(templateH)($scope);
            $("#jsonMakeTable").html(dom);
        })();
        (function() {
            template$API += `
                                                                                                    {
                                                                                                        "url":"${ json.listApi.url }",
                                                                                                        "method": "GET",
                                                                                                        "params":{`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                var name = one.name;
                var type = one.type;
                var api = one.api;
                if(title.indexOf("|")>-1) {
                    var ia = title.split("|");
                    for(var ii = 0, li = ia.length; ii < li; ii++) {
                        template$API += `
                                                                                                            ${ ia[ii] } : '', \/\/ ${ name } | ${ type } | ${ api }`
                    }
                } else {
                    template$API += `
                                                                                                            ${ title } : '', \/\/ ${ name } | ${ type } | ${ api }`
                }
            }
            template$API += `
                                                                                                        },
                                                                                                        "response":{
                                                                                                            "code": "200",
                                                                                                            "msg": "",
                                                                                                            "data":{
                                                                                                                "list":[{`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var name = one.name;
                var type = one.type;
                var mock = one.mock;
                template$API += `
                                                                                                                    "${ title }": "", \/\/ ${ name } | ${ type } | ${ mock }`
            }
            template$API += `
                                                                                                                }],
                                                                                                                "rowcount":100 //条数
                                                                                                            }
                                                                                                        }
                                                                                                    }`
        })();
        var randomId = Math.ceil(Math.random()*1e4);
        // console.info('接口代码生产完毕', templateA);
        // console.info('逻辑代码生产完毕', templateJ);
        // console.info('Dom代码生产完毕', templateH);
        $("#templateA").html( templateA.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') );
        $("#templateAPI").html( template$API.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') );
        $("#templateJ").html(
            `&lt;script type="text/javascript"&gt;\napp.controller("controller${ randomId }", function($scope, $rootScope, $timeout, $alert, $state, $uibModal, yxtable14, $API, $compile) {`+
            templateJ.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n')+
            '\n});\n&lt;/script&gt;'
        );
        $("#templateH").html(
            `&lt;div ng-controller='controller${ randomId }'&gt;`+
            templateH.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') +
            '\n&lt;/div&gt;'
        );
    };
    $scope.makeCodeFile = function() {
        $.ajax({
            url: 'http://localhost:4010/makeJsonFile',
            method: "post",
            data: {
                codea: templateA.replace(/\n[ ]{72}/g, '\n'),
                codej: templateJ.replace(/\n[ ]{72}/g, '\n'),
                codeh: templateH.replace(/\n[ ]{76}/g, '\n')
            }
        });
    };
    ///////////////////////////////
    $scope.frm = {
        a01 : 60, a02 : 15, a02s : [], a03 : '123', a04 : '', a05 : "1496", a06 : "asdf", a07 : 1, a08 : '',
        a09 : 'asdfasdf',
        a10 : 14, a11 : 30, a12 : 15.50, a13 : true, a14 : 0, a15 : "", a16 : [],
        a17 : []
    };
    $scope.frm2 = {
        nameSource: select2Config([])
    };
    var x = [
        {"id":1496,"name":"x109698142(向延杰)"},{"id":1568,"name":"sm1111(郸仁辉)"},
        {"id":10990,"name":"donameg891109(边国栋)"},{"id":19633,"name":"wwqzx9961(黄海锋)"}
    ];
    x.unshift({id:"",name:"全部选择"});
    $scope.frm2.nameSource = select2Config(x, 1);
    $scope.submitFun = throttleSubmit(function(){
        $scope.From.submitted = true;
        if( $scope.From.$invalid ) {
            $alert.error('请检查提交的信息')
            return false;
        }
        var param = {};
        return param
    },function(enable, param){
        console.log('提交代码', param)
        $timeout(function(){
            enable()
        },3e3)
    })
    $scope.a13change = function() {
        if( $scope.From.$invalid ) {
            $scope.From.submitted = true;
            $alert.error("请先填写表单,然后看情况")
            return
        }
        $scope.obj.a13 = !$scope.obj.a13
    };
    $scope.produceFromCode = function() {
        var templateF = '';
        (function() {
            templateF = `
                                                                                                    <form class="yxf14" name="From">
                                                                                                        <b class="yxf14-title">重新整理的表单</b>
                                                                                                        <p><code>[调试开关请忽略]</code><kbd>showDebugger = {{showDebugger}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label">调试开关</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <label class="checkbox-inline i-switch" >
                                                                                                                    <input type="checkbox" ng-model='showDebugger'> <i></i>
                                                                                                                </label>
                                                                                                                <b class="note">请忽略我</b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[必填/数字且0-300]</code><kbd>frm.a01 = {{frm.a01}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">输入框</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <input type="text" placeholder="请输入" style="width:200px;"
                                                                                                                    ng-model="frm.a01" name="a01" maxlength="7"
                                                                                                                    required ui-validate=" '$value>=0&&$value<=300' " ng-change="a01changea02()">
                                                                                                                <b class="note">required ui-validate=" '$value>=0&&$value<=300' "</b>
                                                                                                                <b ng-show="( From.a01.$dirty || From.submitted ) && From.a01.$invalid"> {{From.a01.$error.required?"必填项":"请输入15-300"}} </b>
                                                                                                            </div>
                                                                                                            <div class="yxf14-control" ng-show="( From.a01.$dirty || From .submitted ) && From.a01.$invalid">
                                                                                                                <b>还可以在这里显示错误,{{From.a01.$error.required?"必填项":"请输入15-300"}}</b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[0代表不限/必填/数字且0-300]</code><kbd>frm.a14 = {{frm.a14}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">用户兑换限制</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <input type="number" placeholder="请输入用户兑换限制" style="width:200px;"
                                                                                                                    ng-model="frm.a14" name="o14" maxlength="20" required ui-validate=" '$value>=0&&$value<=300' ">
                                                                                                                <b class="note">0-300, 0代表不限兑！</b>
                                                                                                                <b ng-show="( From.o14.$dirty || From.submitted ) && From.o14.$invalid">
                                                                                                                    {{From.o14.$error.required?"必填项":"请输入0-300"}}
                                                                                                                </b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[必填/字符长度4-20]</code><kbd>frm.a01s = {{frm.a01s}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">输入框</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <input type="text" placeholder="请输入" style="width:200px;"
                                                                                                                    ng-model="frm.a01s" name="a01s" maxlength="21"
                                                                                                                    required ng-minlength="4" ng-maxlength="20">
                                                                                                                <b class="note">required ng-minlength="4" ng-maxlength="20"</b>
                                                                                                                <b ng-show="( From.a01s.$dirty || From.submitted ) && From.a01s.$invalid"> {{From.a01s.$error.required?"必填项":"请输入4-20字符"}} </b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[单选框组]</code><kbd>frm.a02 = {{frm.a02}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">选择</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a02" convert-to-number value="15"> <i></i>15</label>
                                                                                                                <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a02" convert-to-number value="30"> <i></i>30</label>
                                                                                                                <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a02" convert-to-number value="60"> <i></i>60</label>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[复选框组]</code><kbd>frm.a02s = {{frm.a02s}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">选择</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <label class="checkbox-inline i-checks"> <input type="checkbox" checklist-model="frm.a02s" checklist-value="'15'"> <i></i>15</label>
                                                                                                                <label class="checkbox-inline i-checks"> <input type="checkbox" checklist-model="frm.a02s" checklist-value="'30'"> <i></i>30</label>
                                                                                                                <label class="checkbox-inline i-checks"> <input type="checkbox" checklist-model="frm.a02s" checklist-value="'60'"> <i></i>60</label>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[下拉框]</code><kbd>frm.a05 = {{frm.a05}}</kbd>|<kbd>frm.a06 = {{frm.a06}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">下拉框</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <input type="text" select2 style="width:200px;" name="a05"
                                                                                                                    ng-model="frm.a05" config="frm2.nameSource" select2-model="frm2.a05" required
                                                                                                                />
                                                                                                                <b class="note" ng-hide="( From.a05.$dirty || From.submitted ) && From.a05.$invalid">请选择一个你喜欢的</b>
                                                                                                                <b ng-show="( From.a05.$dirty || From.submitted ) && From.a05.$invalid"> 必填项 </b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[文本域]</code><kbd>frm.a09 = {{frm.a09}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">简介</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <textarea placeholder="请输入简介" style="width:400px;"
                                                                                                                    ng-model="frm.a09" name="a09" rows="3" maxlength="64" required>
                                                                                                                </textarea>
                                                                                                                <i>{{ frm.a09.length || 0 }} / 64</i>
                                                                                                                <b ng-show="( From.a09.$dirty || From.submitted ) && From.a09.$invalid"> {{From.a09.$error.required?"必填项":""}} </b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[数字输入框插件]</code><kbd>frm.a11 = {{frm.a11}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">库存</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <input type="text" placeholder="请输入可兑换数量" style="width:200px;"
                                                                                                                    ng-model="frm.a11" name="o11" maxlength="3"
                                                                                                                    codeonly-number required ui-validate="{ a:'$value >= 5', b:'$value <= 50' }">
                                                                                                                <b class="note" ng-hide="( From.o11.$dirty || From.submitted ) && From.o11.$invalid"> 只能输入数字 5-50开区间 </b>
                                                                                                                <b ng-show="( From.o11.$dirty || From.submitted ) && From.o11.$invalid"> {{From.o11.$error.required?"必填项":"5-50开区间"}} </b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[数字小数输入框插件]</code><kbd>frm.a12 = {{frm.a12}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label must">市面价格</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <input type="text" placeholder="请输入数字" style="width:200px;"
                                                                                                                    ng-model="frm.a12" name="o12" maxlength="4" 
                                                                                                                    codeonly-digital codeonly-digital-dot="2" required ui-validate="{ a:'$value >= 10', b:'$value <= 50' }">
                                                                                                                <b class="note">元</b>
                                                                                                                <b class="note" ng-hide="( From.o12.$dirty || From.submitted ) && From.o12.$invalid"> 只能输入数字 小数点 10-50开区间 </b>
                                                                                                                <b ng-show="( From.o12.$dirty || From.submitted ) && From.o12.$invalid"> {{From.o12.$error.required?"必填项":"10-50开区间"}} </b>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[开关]</code><kbd>frm.a13 = {{frm.a13}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label">是否上架</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <label class="checkbox-inline i-switch">
                                                                                                                    <input type="checkbox" ng-model='frm.a13'> <i></i>
                                                                                                                </label>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <p ng-show="showDebugger"><code>[开关]</code><kbd>frm.a13 = {{frm.a13}}</kbd></p>
                                                                                                        <div class="yxf14-box">
                                                                                                            <label class="yxf14-label">是否上架</label>
                                                                                                            <div class="yxf14-control">
                                                                                                                <span class="checkbox-inline i-switch" ng-click="a13change()">
                                                                                                                    <input type="checkbox" ng-checked='frm.a13' disabled> <i></i>
                                                                                                                </span>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                        <!-- 提交 -->
                                                                                                        <div class="yxf14-box">
                                                                                                            <div class="yxf14-control">
                                                                                                                <button type="button" class="btn btn-danger" ng-click="submitFun($event)">提交表单</button>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                    </form>
            `
            var dom = $compile(templateF)($scope);
            $("#jsonMakeFrom").html(dom);
        })();
        $("#templateF").html( templateF.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') );
    };
    $scope.printFromObj = (function() {
        var str = JSON.stringify($scope.fromObj);
        console.log( str )
        var storage = window.localStorage;
        storage.setItem("makeTable12Code", str);
    });
    $scope.getFromObj = (function() {
        if( window.hahaha ){
            $scope.fromObj = json = window.hahaha;
        } else {
            var storage = window.localStorage;
            $scope.fromObj = json = eval( '('+storage.getItem("makeTable12Code")+')' );
        }
    });
}
